package com.ethan.minimalist.ui

import androidx.fragment.app.FragmentTransaction
import com.cxz.kotlin.baselibs.base.BaseActivity
import com.cxz.kotlin.baselibs.ext.showToast
import com.ethan.minimalist.R
import com.ethan.minimalist.bean.TabEntity
import com.ethan.minimalist.ui.home.HomeFragment
import com.ethan.minimalist.ui.mine.MineFragment
import com.ethan.minimalist.ui.novel.NovelFragment
import com.ethan.minimalist.ui.sign.SignInFragment
import com.ethan.minimalist.ui.video.VideoFragment
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.gyf.immersionbar.ImmersionBar
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : BaseActivity() {

    private val mTitles = arrayOf("百度","好看视频","小说","去签到","我的")
    private val mIconUnSelectIds = intArrayOf(
        R.mipmap.icon_bd_unselect,
        R.mipmap.icon_tab_video_normal,
        R.mipmap.icon_tab_novel_normal,
        R.mipmap.icon_browse_normal,
        R.mipmap.icon_tab_mine_normal
    )
    private val mIconSelectIds = intArrayOf(
        R.mipmap.icon_bd_select,
        R.mipmap.icon_tab_video_select,
        R.mipmap.icon_tab_novel_select,
        R.mipmap.icon_browse_select,
        R.mipmap.icon_tab_mine_select
    )
    private val mTabEntities = ArrayList<CustomTabEntity>()

    private var mHomeFragment: HomeFragment?= null
    private var mVideoFragment: VideoFragment?= null
    private var mNovelFragment: NovelFragment?= null
    private var mSignInFragment: SignInFragment?= null
    private var mMineFragment: MineFragment?= null

    //默认为0
    private var mIndex:Int = 0

    override fun attachLayoutRes(): Int {
        return R.layout.activity_main
    }

    override fun initData() {
        super.initData()
    }

    override fun initView() {
        ImmersionBar.with(this@MainActivity)
            .statusBarView(top_view)
            .statusBarDarkFont(true, 0.2f)
            .init()

        initTab()
        tab_layout?.isIndicatorAnimEnable = false
        tab_layout?.currentTab = mIndex
        switchFragment(mIndex)
    }

    private fun initTab() {
        (mTitles.indices)
            .mapTo(mTabEntities) { TabEntity(mTitles[it], mIconSelectIds[it], mIconUnSelectIds[it]) }
        tab_layout.setTabData(mTabEntities)
        tab_layout.setOnTabSelectListener(object :OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                //切换Fragment
                switchFragment(position)
            }

            override fun onTabReselect(position: Int) {

            }
        })
    }

    override fun start() {

    }

    private fun switchFragment(position: Int) {
        var realPos = position
        if(realPos == 3) {
            showToast("签到页：请升级版本")
            realPos = mIndex
        }
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)

        when(realPos) {
            0 -> mHomeFragment?.let {
                transaction.show(it)
            } ?:HomeFragment.getInstance(mTitles[position])
                .let {
                    mHomeFragment = it
                    transaction.add(R.id.fl_container,it,"home")
                }

            1 -> mVideoFragment?.let {
                transaction.show(it)
            } ?:VideoFragment.getInstance(mTitles[position])
                .let {
                    mVideoFragment = it
                    transaction.add(R.id.fl_container,it,"video")
                }

            2 -> mNovelFragment?.let {
                transaction.show(it)
            } ?:NovelFragment.getInstance(mTitles[position])
                .let {
                    mNovelFragment = it
                    transaction.add(R.id.fl_container,it,"novel")
                }

            3 -> mSignInFragment?.let {
                transaction.show(it)
            } ?:SignInFragment.getInstance(mTitles[position])
                .let {
                    mSignInFragment = it
                    transaction.add(R.id.fl_container,it,"sign")
                }

            4 -> mMineFragment?.let {
                transaction.show(it)
            } ?:MineFragment.getInstance(mTitles[position])
                .let {
                    mMineFragment = it
                    transaction.add(R.id.fl_container,it,"mine")
                }
        }

        mIndex = realPos
        tab_layout.currentTab = mIndex
        transaction.commitAllowingStateLoss()
    }

    private fun hideFragments(transaction: FragmentTransaction) {
        mHomeFragment?.apply {
            transaction.hide(this)
        }
        mVideoFragment?.apply {
            transaction.hide(this)
        }
        mNovelFragment?.apply {
            transaction.hide(this)
        }
        mSignInFragment?.apply {
            transaction.hide(this)
        }
        mMineFragment?.apply {
            transaction.hide(this)
        }
    }
}
